﻿@model System.Collections.Generic.IEnumerable<Kooboo.CMS.Sites.Models.Page>
@{
    ViewBag.Title = "Pages".Localize();
    Layout = "~/Views/Shared/Site.cshtml";
    var sortable = string.IsNullOrEmpty(ViewContext.RequestContext.GetRequestValue("sortField"));

    var parentPage = (Kooboo.CMS.Sites.Models.Page)ViewBag.Page;
}
@section Panel{
    <ul class="panel">
        @if (parentPage != null && parentPage.IsLocalized(Site.Current))
        {
            <li>
                <a href="@Url.Action("Edit", ViewContext.RequestContext.AllRouteValues().Merge("UUID", parentPage.FullName).Merge("return", Request.RawUrl))">
                    @Html.IconImage("edit")
                    @("Edit".Localize())
                </a>
            </li>
        }
        <li class="j_DropDown">
            <a>
                @Html.IconImage("add")
                @("Create New".Localize())
                @Html.IconImage("arrow small white-down")
            </a>
            @Html.Partial("_LayoutList", ViewData["LayoutList"])
        </li>
        <li>
            <a href="@Url.Action("Import", ViewContext.RequestContext.AllRouteValues().Merge("return", ViewContext.HttpContext.Request.RawUrl).Merge("title", "Import pages"))">
                @Html.IconImage("import") @("Import".Localize())
            </a>
        </li>
        @if (ServiceFactory.UserManager.Authorize(Kooboo.CMS.Sites.Models.Site.Current, ViewContext.HttpContext.User.Identity.Name, Kooboo.CMS.Account.Models.Permission.Sites_Page_PublishPermission))
        {
            <li>
                <a id="delete_bar" href="@Url.Action("Delete", ViewContext.RequestContext.AllRouteValues().Merge("return", ViewContext.HttpContext.Request.RawUrl))" data-show-on-check="Any" data-show-on-selector=".localized" data-command-type="AjaxPost" data-confirm="@("Are you sure you want to delete these items?".Localize())">
                    @Html.IconImage("delete") @("Delete".Localize())
                </a>
            </li>

            <li>
                <a href="@Url.Action("Localize", ViewContext.RequestContext.AllRouteValues().Merge("return", ViewContext.HttpContext.Request.RawUrl))" data-show-on-check="Any" data-show-on-selector=".unlocalized" data-command-type="AjaxPost">
                    @Html.IconImage("localize") @("Localize".Localize())
                </a>
            </li>
        }
        @foreach (var group in Kooboo.CMS.Sites.Extension.UI.TopToolbar.ToolbarButtons.GetToolbarButtons(ViewContext.RequestContext))
        {
            @Html.Partial("_ToolbarGroupButtons", group)
        }
    </ul>
}
<div class="topbar clearfix">
    <h1 class="title">
        @if (parentPage != null)
        {
            @parentPage.LinkText
        }
        else
        {
            @ViewBag.Title
        }
    </h1>
    @if (parentPage != null)
    {
        var path = parentPage.PageNamePaths.ToArray();
        <div class="breadcrumb">
            <a href="@Url.Action("Index", ViewContext.RequestContext.AllRouteValues().Merge("parentPage", ""))">@ViewBag.Title</a>
            @for (var i = 0; i < path.Length; i++)
            {
                @Html.IconImage("arrow gray-right")
                var current = new Kooboo.CMS.Sites.Models.Page(Site.Current, PageHelper.CombineFullName(path.Take(i + 1))).LastVersion(Site.Current).AsActual();
                if (i == path.Length - 1)
                {
                    <span>@current.LinkText</span>
                }
                else
                {
                    <a href="@Url.Action("Index", ViewContext.RequestContext.AllRouteValues().Merge("parentPage", @current.FullName))">@current.LinkText</a>
                }
            }
        </div>
    }
    @Html.Partial("_Search")
</div>
@Html.GridForModel2()
<script>
    @if (sortable)
    {
        <text>
    $(function () {
        var fixHelper = function (e, ui) {
            ui.children().each(function () {
                $(this).width($(this).width());
            });
            return ui;
        };
        $("div.common-table table tbody tr").css("cursor", "move");
        $("div.common-table table tbody").sortable({
            helper: fixHelper,
            handle: 'td.draggable .drag',
            update: function (event, ui) {
                var pageNameOrders = {};
                _.each($('[data-pagename]').toArray(), function (item, index) {
                    var $item = $(item);
                    var p = 'pageNames[' + index + ']';
                    pageNameOrders[p] = $item.data('pagename');
                });

                $.ajax({
                    url: '@Html.Raw(this.Url.Action("SortPages", ViewContext.RequestContext.AllRouteValues()))',
                    data: pageNameOrders,
                    type: 'post',
                    success: function (response) {
                        parse_JsonResultData(response);
                    },
                    error: function () {
                    }
                });
            }
        }).disableSelection();
    });
    </text>
    }
    else
    {
        <text>
    $('img.drag').remove();
    </text>
    }
</script>

<script>
    $(function () {
        var deleteBar = $('#delete_bar');
        var unpublishBar = $('#unpublish_bar');
        var changeMessage = function () {
            var homepageChecked = $('.homepage.active').length != 0;
            console.log($('.homepage.active').length);
            if (homepageChecked) {
                deleteBar.data("confirm", '@("One of the pages you want to unpublish is set as homepage.Are you sure you want to delete these items?".Localize())');
                unpublishBar.data('confirm', '@("One of the pages you want to unpublish is set as homepage.Are you sure you want to unpublish these items?".Localize())');
            }
            else {
                deleteBar.data("confirm", '@("Are you sure you want to delete these items?".Localize())');
                unpublishBar.data('confirm', '@("Are you sure you want to unpublish these items?".Localize())');
            }
        };
        $('.common-table.fixed tr').click(changeMessage);
    })
</script>
